#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/9/3 15:10
# @Author : 吴敬超
# explain:
import pandas as pd
import threading
import numpy as np
import netCDF4 as nc
import datetime
import csv
import time as ti
import glob

columnsTitles = ['z_t', 'lat', 'lon', 'SALT']


def csvhead(csvpath, csvfilehead):
    csvheadpath = csvpath + csvfilehead
    print csvheadpath
    with open(csvheadpath, mode='wb') as ice_file:
        ice_writer = csv.writer(ice_file, dialect='excel')
        ice_writer.writerow(columnsTitles)
    ice_file.close()



def csvmerger(csvpath, csvtp, csvsoufile):
    globcsvpath = csvpath + csvtp
    csvx_list = glob.glob(globcsvpath)
    sourcepath = csvpath + csvsoufile
    print('总共发现%s个CSV文件' % len(csvx_list))
    # time.sleep(2)
    print('正在处理............')
    for i in csvx_list:
        # print str(csvx_list[i])
        fr = open(i, 'r').read()
        with open(sourcepath, 'a') as f:
            f.write(fr)
        print('写入成功！')
    print('写入完毕！')

# def func(msg):
#     for i in xrange(3):
#         print msg
#         time.sleep(1)


if __name__ == "__main__":
    # print("main")

    print "开始生成csv头文件"

    csvpathsou = "D:/fiecsv/merge/"
    csvsouhe = "headsvalt6file.csv"
    csvtp = "svalt*.csv"

    csvhead(csvpathsou, csvsouhe)

    csvmerger(csvpathsou, csvtp, csvsouhe)

    print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())
